package com.gdface.admin.gdface_attend;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.pefis.natives.Ast2600JNI;
import com.pefis.natives.PfsBioInfo;

import com.gdface.admin.gdface_attend.listView.Adapter_User;
import com.gdface.admin.gdface_attend.listView.Custom_User;
import com.gdface.admin.gdface_attend.listView.HOnScrollListener;
import com.gdface.admin.gdface_attend.utils.DaoUtils;

import net.gdface.face.Entity.Att_user;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/1/8.
 */

public class Show_User_Activity  extends AppCompatActivity implements HOnScrollListener.OnloadDataListener,Custom_User {

    private ListView listView;

    private Adapter_User adapter;
    private View footView;//底部加载更多布局
    private View headView;//头部加载显示布局
    private List<Att_user> data;//开门数据
    private int page = 0;//数据的总共页数
    private int page_Currently = 0;//当前加载的页数
    private int page_sum = 20;//每页加载的item个数
    private List<Att_user> list_data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_user);


        getData();
        listView = (ListView) findViewById(R.id.listView_shoe_user);


        footView = LayoutInflater.from(Show_User_Activity.this).inflate(R.layout.foot_boot, null);
        footView.setVisibility(View.GONE);
        listView.addFooterView(footView);
        adapter = new Adapter_User(data,Show_User_Activity.this);
        listView.setAdapter(adapter);

        //自定义滚动时间监听
        HOnScrollListener scrollListener = new HOnScrollListener(footView);
        //設置回掉接口
        scrollListener.setOnLoadDataListener(this);
        //设置ListView的滚动监听事件
        listView.setOnScrollListener(scrollListener);

    }

    private void getData() {
        list_data = DaoUtils.findUser();
        Log.i("hwwlog","list size = "+list_data.size());
        data = new ArrayList<Att_user>();
        page = 0;
        if (null != list_data && 0 < list_data.size()) {//判断list是否有数据
            page = list_data.size() / page_sum + 1;

            if (list_data.size() > page_sum) {//判断list数据长度是否大 指定的item个数
                for (int i = 0; i < page_sum; i++) {
                    data.add(list_data.get(i));
                }
            } else {
                data = list_data;
            }
            page_Currently = 1;
        } else {
            data = null;
            Toast.makeText(this, "无用户数据!", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onLoadData() {
        if (0 != page) {
            Log.i("hwwlog","page = "+page+"   page_Currently = "+page_Currently);
            if (page >= page_Currently) {
                if (page - page_Currently > 1) {   //加载的页数不是最后一页
                    for (int i = page_sum * page_Currently; i < (page_Currently + 1) * page_sum; i++) {
                        data.add(list_data.get(i));
                    }
                } else {   //加载最后一页的数据
                    for (int i = page_Currently * page_sum; i < list_data.size(); i++) {
                        data.add(list_data.get(i));
                    }
                }
                adapter.notifyDataSetChanged();
            }
            page_Currently++;
        }
    }

    public  void adapterNotify(){
        adapter.notifyDataSetChanged();
    }

    @Override
    public void DeleteUser(long id) {
        DaoUtils.deleteUserByKey(id);
        DeleteUserShow(Integer.valueOf(String.valueOf(id)));
        for(int i = 0;i<list_data.size();i++){
            if(list_data.get(i).getId() == id){
                list_data.remove(i);
            }
        }
        adapterNotify();
    }


    public void DeleteUserShow(int uID){
        int errcode;
        File mDeleteUserData;
        mDeleteUserData = new File(PfsBioInfo.TEMPLATE_DB_PATH+uID);
        if(mDeleteUserData.exists()){
            mDeleteUserData.delete();
        }

        mDeleteUserData = new File(PfsBioInfo.PHOTO_DB_PATH+uID+".JPEG");
        if(mDeleteUserData.exists()){
            mDeleteUserData.delete();
        }
        errcode = Ast2600JNI.getInstance().pisFcDeleteAtTptArray(PfsBioInfo.getInstance().mContextID, uID);
        if(errcode != 0){
            Log.i("hwwlog","pisFcDeleteAtTptArray  fail");
            return;
        }
    }

}
